package ar.uba.fi.taller2.ftrs.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;

import ar.uba.fi.taller2.ftrs.repo.RepositoryManager;


@SuppressWarnings("serial")
public class DialogoAgregar extends JDialog
{
	private JComboBox jComboBoxRepositorio;
	private JTextField jTextFieldPath;
	private JButton buttonExaminar;
	
	private JButton buttonOk;
	private JButton buttonCancel;
	
	private Action accionAceptar;
	private Action accionCancelar;
	private Action accionExaminar;
	
	private JPanel jPanelNewPrincipal;
	private JPanel jPanelNewBotonera;
	
	private JFrame frmPadre;
	
	public DialogoAgregar(JFrame framePadre)
	{
		super(framePadre, "Agregar Documento", true);
		
		
		frmPadre=framePadre;
		
		setLayout(new GridBagLayout());
		
		jComboBoxRepositorio= new JComboBox();
		String [] items = ((Ventana)frmPadre).getRepositorios().listRepositories();
		for (int k=0;k<items.length;k++)
			jComboBoxRepositorio.addItem(items[k]);
		jTextFieldPath= new JTextField(40);
		
		//Boton Aceptar
		accionAceptar = new AbstractAction("Aceptar") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Aceptar");
			}
			public void actionPerformed(ActionEvent e)
			{
			try {					
					DialogoAgregar.this.setVisible(false);
					((Ventana)frmPadre).getRepositorios().addDocument((String)jComboBoxRepositorio.getSelectedItem(), jTextFieldPath.getSelectedText());
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		};
		buttonOk= new JButton(accionAceptar);
		
		//Boton Cancelar
		accionCancelar = new AbstractAction("Cancelar") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Cancelar");
			}
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		};
		buttonCancel= new JButton(accionCancelar);
		
		//Boton Examinar
		accionExaminar = new AbstractAction("Examinar...") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Examinar...");
			}
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser chooser = new JFileChooser();
			    FiltroArchivos ff = new FiltroArchivos();
				
			    chooser.setFileFilter(ff);
			    int returnVal = chooser.showOpenDialog(frmPadre);
			    if(returnVal == JFileChooser.APPROVE_OPTION)
			    	jTextFieldPath.setText(chooser.getSelectedFile().getAbsolutePath() );
			}
		};
		buttonExaminar= new JButton(accionExaminar);
		
		jPanelNewPrincipal= new JPanel();
		jPanelNewBotonera= new JPanel();
		
		jPanelNewPrincipal.setLayout(new GridBagLayout());
		jPanelNewBotonera.setLayout(new GridBagLayout());
		
		//Se llena el jPanelNewPrincipal
		GridBagConstraints c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		int y = 0;
		c.weightx = 0;
		c.insets = new Insets(3, 6, 3, 6);
		jPanelNewPrincipal.add(new JLabel("Repositorio:"), c);
		
		c.weightx = 1;
		jPanelNewPrincipal.add(jComboBoxRepositorio, c);
		
		c.gridy = ++y;
		
		c.weightx = 0;
		jPanelNewPrincipal.add(new JLabel("Path:"), c);
		
		c.weightx = 1;
		jPanelNewPrincipal.add(jTextFieldPath, c);
		
		c.weightx = 0;
		jPanelNewPrincipal.add(buttonExaminar, c);		
		
		c.gridy = ++y;
		
		c.weightx = 0;
		//	jPanelNewPrincipal.add(jCheckBoxUrlPath , c);
				
		//Se llena el jPanelNewBotonera
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.insets = new Insets(3, 3, 3, 3);
		jPanelNewBotonera.add(buttonOk, c);
		c.insets = new Insets(3, 3, 3, 3);
		jPanelNewBotonera.add(buttonCancel, c);
		
		//Se llena el dialogoNew
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		add(jPanelNewPrincipal, c);
		
		c.gridy = 1;
		c.weightx = 1;
		c.weighty = 0;
		c.fill=GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		add(jPanelNewBotonera, c);
		
		JRootPane rootPane = getRootPane();
		rootPane.setDefaultButton(buttonOk);
		rootPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "salimo");
		rootPane.getActionMap().put("salimo", new AbstractAction() {
			public void actionPerformed(ActionEvent e)
			{
				setVisible(false);
			}
		});
		pack();
		setLocationRelativeTo(framePadre);
	}
	
	public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
	
	class FiltroArchivos extends FileFilter
	{
		@Override
		public boolean accept(File arg0)
		{
			if (arg0.isDirectory())
				return true;
			
			String extension = getExtension(arg0);
			
			if(extension!=null)
			{
				if(extension.equals("htm"))
					return true;
				if(extension.equals("html"))
					return true;
			}
			
			return false;
		}

		@Override
		public String getDescription()
		{
			return "Archivos de pgina web (.html, .htm)";
		}
		
	}
}
